package steed.hadmin.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import steed.action.annotation.Power;
import steed.domain.GlobalParam;
import steed.domain.system.AdminUser;
import steed.exception.PathIsTopException;
import steed.ext.domain.system.Menu;
import steed.hibernatemaster.util.DaoUtil;
import steed.util.base.CollectionsUtil;
import steed.util.base.PathUtil;
import steed.util.base.StringUtil;

@Namespace("/hadmin")
@ParentPackage(value="steed")
@InterceptorRefs({@InterceptorRef(value="mydefault")})
@Power("登录后台")
public class IndexAction extends HadminAction{
	private static final long serialVersionUID = 4395374259028028362L;
	@Action(value="index")
	public String index(){
		if (getSession().getAttribute("menuList") == null) {
			AdminUser user = getLoginUser();
			List<String> asc = new ArrayList<String>();
			asc.add("order1");
			Menu t = new Menu();
			t.setShow(true);
			List<Menu> menuList = DaoUtil.listAllObj(t, null, asc);
			
			Iterator<Menu> iterator = menuList.iterator();
			
			Map<String, Power> scanActionPower = (Map<String, Power>)getServletContext().getAttribute("path_powerMap");
			//删除用户没有权限的菜单
			while (iterator.hasNext()) {
				Menu next = iterator.next();
				if (next.getId() < 0) {
					continue;
				}
				String url = next.getUrl();
				if (StringUtil.isStringEmpty(url)) {
					continue;
				}
				if (url.startsWith(".")) {
					url = url.substring(1);
				}
				Power power = scanActionPower.get(url);
				if (power != null && !user.hasPower(power.value())) {
					iterator.remove();
					continue;
				}
				//检查是否有父类路径权限
				try {
					String paraentPath = url;
					while ((paraentPath = PathUtil.getParaentPath(paraentPath)) != null) {
						Power power2 = scanActionPower.get(paraentPath+"/");
						if (power2 != null && !user.hasPower(power2.value())) {
							iterator.remove();
							break;
						}
					}
				} catch (PathIsTopException e) {
				}
			}
			
			List<Menu> newMenuList = new ArrayList<Menu>();
			for (Menu m : menuList) {
				Integer fatherId = m.getFatherId();
				if (fatherId == 0) {
					newMenuList.add(m);
				} else {
					menuList.get(menuList.indexOf(new Menu(fatherId))).addSon(m);
				}
			}
			removeEmptyMenu(newMenuList);
			
			setSessionAttribute("menuList", newMenuList);
		}
		return steed_forward;
	}
	/**
	 * 移除没有子节点且没有url值的菜单
	 * @param newMenuList
	 */
	private void removeEmptyMenu(List<Menu> newMenuList) {
		Iterator<Menu> i = newMenuList.iterator();
		while (i.hasNext()) {
			Menu temp = i.next();
			//用户就是上帝，用户自定义的即使是空也不能删
			if (temp.getId() < 0) {
				continue;
			}
			removeEmptyMenu(temp.getSonList());
			if (StringUtil.isStringEmpty(temp.getUrl()) && CollectionsUtil.isListEmpty(temp.getSonList())) {
				i.remove();
			}
		}
	}
	
	@Action(value="logout",results={@Result(name="success",location="/WEB-INF/jsp/hadmin/login.jsp")})
	public String logout(){
		getSession().removeAttribute(GlobalParam.attribute.admin);
		getSession().removeAttribute(GlobalParam.attribute.user);
		getSession().removeAttribute("menuList");
		return SUCCESS;
	}
	@Override
	protected String[] getListFields() {
		return null;
	}
}
